
/**
 * <op>	::= <no> | <id> | (<exp>)
 * 
 * Op Class. Consists of either a number, an ID, or an expression.
 */
public class Operands {

	private int number;
	private String id;
	private Expressions expression;

	public Operands(Tokens tokens){
		this.id = null;
		this.expression = null;
		this.parse(tokens);
	}
	
	public void parse(Tokens tokens){
		// check for "("
		if(tokens.getToken() == 20){
			this.expression = new Expressions(tokens);
			if(tokens.getToken() != 21){
				System.err.println("Keyword ')' expected before token :" + tokens.idName());
				System.exit(0);
			} else {
				tokens.skipToken();
			}
		} else if(tokens.getToken() == 31){ // check for an integer
			String tmp = tokens.idName();
			this.number = Integer.parseInt(tmp);
			tokens.skipToken();
		} else if(tokens.getToken() == 32){ // check for an ID
			this.id = tokens.idName();
			tokens.skipToken();
		}
	}

	public int runOp() {
		int value;
		if(this.expression != null){
			value = this.expression.runExp();
		} else if(this.id != null){
			value = Id.getVal(this.id);
		} else {
			value = this.number;
		}
		return value;
	}
}